#!/bin/sh
#/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#/* Copyright 2002-2003 by Mark Wolgemuth, All rights reserved.             */
#/* Copyright 2002-2003 by Employease, Inc.  All rights reserved.           */
#/*                                                                         */
#/* Redistribution and use in source and binary forms, with or without      */
#/* modification, are permitted provided that the following conditions      */
#/* are met:                                                                */
#/*                                                                         */
#/*    1. Redistributions of source code must retain the above copyright    */
#/* notice, this list of conditions and the following disclaimer.           */
#/*    2. Redistributions in binary form must reproduce the above copyright */
#/* notice, this list of conditions and the following disclaimer in the     */
#/* documentation and/or other materials provided with the distribution.    */
#/*                                                                         */
#/* THIS SOFTWARE IS PROVIDED BY THE MOD_ATHENA PROJECT ``AS IS'' AND ANY   */
#/* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE       */
#/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR      */
#/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR        */
#/* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   */
#/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,     */
#/* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR      */
#/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     */
#/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT            */
#/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   */
#/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    */
#/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

ATH_AGENT_SH_VERSION="ath_agent.sh 0.1 2003/02/06 (C) mark wolgemuth"

## search down to "main begin" for logic flow
##

if [ "${ATH_ETC_DIR}" = "" ]
then
    ATH_ETC_DIR="."
fi
. ${ATH_ETC_DIR}/ath_shell_cmds.sh

## XXX not adjusted to handle new fields (cus 0-9 and disk)

## changeable var defs
# change these to paths to your apps that output 1 number
# for the different optional custom vars
# eg. "lynx -dump http://myapp/mystat.pl"
# eg. "/usr/local/bin/mywackywrapper.sh"
ATH_1CUS_CMD=""
ATH_2CUS_CMD=""
ATH_3CUS_CMD=""

## internal vars

ATH_PID_FILE="/tmp/ath_agent_sh.pid"
GET_CMD=""
AGENT_OPTS=""
HOST_TYPE=`uname`
STAT_STR=""
ATH_OPT_VERBOSE=""
ATH_OPT_QUIET=""
ATH_OPT_BG="NO"
ATH_OPT_LOG=""
ATH_OPT_BURL=""
ATH_OPT_FREQ=""
ATH_OPT_HOST=""
ATH_OPT_LOGFILE=""
ATH_OPT_UPDATES=""
ATH_OPT_NOSEND=""

## function defs

# usage() prints usage
usage() {
    echo "ath_agent.sh [-v] [-q] [-b] [-?] [-u BASEURL] [-f frequency]"
    echo "             [-h hostname] [-s send_update_string]"
    echo ""
    echo "  -v       Prints version and exits"
    echo "  -q       Suppresses all status and warning messages (quiet)"
    echo "  -b       Runs in background"
    echo "  -u (url) Specifies base URL to reach the athena engine"
    echo "  -f (sec) Sets update frequency (default to 5)"
    echo "  -h (nam) Sets name to send in member update (default to hostname)"
    echo "  -F (str) Comma separated list of fields to send in update"
    echo "             eg. \"cpu,mem,net,ld,1cus,2cus,3cus\""
    echo "  -n       Don't send reqs, just print what would be sent to STDOUT"
    echo "  -k       Kill the currently running daemon and exit"
    return 0
}

# set_opts() runs getopts and sets defaults
set_opts() {
    while getopts "vVqblnu:f:h:F:o:k" OPTTYPE; do
	case ${OPTTYPE} in
	"v")
	    echo "${ATH_AGENT_SH_VERSION}"
	    exit
	    ;;
	"V") 
	    ATH_OPT_VERBOSE="1"
	    ;;
	"q")
	    ATH_OPT_QUIET="1"
	    ;;
	"b")
	    ATH_OPT_BG="YES"
	    ;;
	"l")
	    ATH_OPT_LOG="1"
	    ;;
	"u")
	    ATH_OPT_BURL="${OPTARG}"
	    ;;
	"f")
	    ATH_OPT_FREQ="${OPTARG}"
	    ;;
	"h")
	    ATH_OPT_HOST="${OPTARG}"
	    ;;
	"F")
	    ATH_OPT_UPDATES=`echo ${OPTARG} | sed -e 's/,/ /g'`
	    ATH_HAVE_UPTIME=`which uptime`
	    ;;
	"o")
	    ATH_OPT_LOGFILE="${OPTARG}"
	    touch ${ATH_OPT_LOGFILE} > /dev/null 2>&1
	    if (test ! -e ${ATH_OPT_LOGFILE}); then
		echo "Cannot create log file: ${ATH_OPT_LOGFILE}"
	    fi
	    ;;
	"n")
	    ATH_OPT_NOSEND="1"
	    ;;
	"k")
	    kill `cat ${ATH_PID_FILE}`
	    exit
	    ;;
	[?])
	    usage
	    exit
	    ;;
	esac
    done
    # do defaults
    if [ "${ATH_OPT_BURL}" = "" ]; then
	status_msg "WARNING: using default base athena engine URL"
	ATH_OPT_BURL="http://localhost:8080/ath"
    fi
    if [ "${ATH_OPT_HOST}" = "" ]; then
	ATH_OPT_HOST=`hostname`
    fi
    if [ "${ATH_OPT_FREQ}" = "" ]; then
	ATH_OPT_FREQ="5"
    fi
    if [ "${ATH_OPT_UPDATES}" = "" ]; then
	ATH_OPT_UPDATES="cpu"
    fi
    return 0
}


# get_stats() gathers apropriate stats and builds string to send
get_stats() {
    STAT_STR=""
    STAT_CPU=""
    STAT_NET=""
    STAT_MEM=""
    STAT_LD=""
    STAT_1CUS=""
    STAT_2CUS=""
    STAT_3CUS=""

    for STAT_OPT in $ATH_OPT_UPDATES; do
	case "$STAT_OPT" in
	"cpu")
	    if [ "$HOST_TYPE" = "FreeBSD" ]; then
		STAT_CPU=`vmstat -p proc 1 2 |tail -1 |awk '{ print 100 - $17; }'`
	    elif [ "$HOST_TYPE" = "Linux" ]; then
		STAT_CPU=`vmstat 1 2 |tail -1 |awk '{ print 100 - $16; }'`
	    elif [ "$HOST_TYPE" = "SunOS" ]; then
		STAT_CPU=`vmstat 1 2 |tail -1 |awk '{ print 100 - $22; }'`
	    elif [ "$HOST_TYPE" = "OSF1" ]; then
		STAT_CPU=`vmstat 1 2 |tail -1 |awk '{ print 100 - $18; }'`
	    else
		STAT_CPU="0"
	    fi
	    STAT_STR="${STAT_STR},c=${STAT_CPU}"
	    ;;
	"net")
	    STAT_NET="-1"
	    STAT_STR="${STAT_STR},n=${STAT_NET}"
	    ;;
	"mem")
	    STAT_MEM="-1"
	    STAT_STR="${STAT_STR},n=${STAT_MEM}"
	    ;;
	"ld")
	    if [ "$ATH_HAVE_UPTIME" = "" ]; then
		STAT_LD="-1"
	    else
		STAT_LD=`$ATH_HAVE_UPTIME | awk -F": " '{ print $2; }' | awk -F", " '{ print $1; }'`
	    fi
	    STAT_STR="${STAT_STR},l=${STAT_LD}"
	    ;;
	"1cus")
	    if [ "${ATH_1CUS_CMD}" != "" ]; then
		STAT_1CUS = `${ATH_1CUS_CMD}`
		STAT_STR="${STAT_STR},1=${STAT_1CUS}"
	    fi
	    ;;
	"2cus")
	    if [ "${ATH_2CUS_CMD}" != "" ]; then
		STAT_2CUS = `${ATH_2CUS_CMD}`
		STAT_STR="${STAT_STR},2=${STAT_2CUS}"
	    fi
	    ;;
	"3cus")
	    if [ "${ATH_3CUS_CMD}" != "" ]; then
		STAT_3CUS = `${ATH_3CUS_CMD}`
		STAT_STR="${STAT_STR},3=${STAT_3CUS}"
	    fi
	    ;;
	esac
    done
}

# send_update() sends update to athena engine
send_update() {
    GET_CMD="${AGENT_DIR}/${AGENT_BIN} ${AGENT_OPTS} \
	${ATH_OPT_BURL}/update/phys?h=${ATH_OPT_HOST}${STAT_STR}"
    if [ "${ATH_OPT_VERBOSE}" = "1" -o "${ATH_OPT_NOSEND}" = "1" ]; then
	status_msg $GET_CMD
    fi
    if [ "${ATH_OPT_NOSEND}" != "1" ]; then
	$GET_CMD > /dev/null
    fi
}

loop_run() {
    get_stats
    send_update
}

## main begin ##
################

setup
set_opts $@
chk_agent_bin
add_agent_opts
exec_handler

################
### main end ###
